package com.mrzhou.log.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrzhou.core.tool.api.R;
import com.mrzhou.core.tool.utils.BeanUtil;
import com.mrzhou.log.annotation.ApiLog;
import com.mrzhou.log.annotation.ApiLogIgnore;
import com.mrzhou.log.entity.LogApi;
import com.mrzhou.log.entity.LogError;
import com.mrzhou.log.entity.LogUsual;
import com.mrzhou.log.req.PageLogApiReq;
import com.mrzhou.log.service.ILogApiService;
import com.mrzhou.log.service.ILogErrorService;
import com.mrzhou.log.service.ILogUsualService;
import com.mrzhou.mybatis.base.PageQuery;
import com.mrzhou.mybatis.support.Condition;
import com.mrzhou.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/log")
@Api(tags = "日志记录保存接口")
@ApiLogIgnore
public class LogController {

    @Autowired
    private ILogApiService logApiService;
    @Autowired
    private ILogErrorService logErrorService;
    @Autowired
    private ILogUsualService logUsualService;

    @PostMapping("page-api")
    @ApiOperation("分页查询请求日志")
    public R<IPage<LogApi>> pageLogApi(PageLogApiReq query) {
        LambdaQueryWrapper<LogApi> wrapper = Condition.getQueryWrapper(query, LogApi.class).orderByDesc(LogApi::getCreateTime);
        return R.data(logApiService.page(new Page<>(query.getCurrent(), query.getSize()), wrapper));
    }

    @PostMapping("api/submit")
    @ApiOperation("保存接口请求日志")
    @ApiLogIgnore
    public R<String> apiSubmit(@RequestBody LogApi logApi) {
        return R.status(logApiService.saveOrUpdate(logApi));
    }

    @PostMapping("error/submit")
    @ApiOperation("保存项目中运行时产生的异常日志")
    @ApiLogIgnore
    public R<String> errorSubmit(@RequestBody LogError logError) {
        return R.status(logErrorService.saveOrUpdate(logError));
    }

    @PostMapping("usual/submit")
    @ApiOperation("保存通用日志记录")
    @ApiLogIgnore
    public R<String> usualSubmit(@RequestBody LogUsual logUsual) {
        return R.status(logUsualService.saveOrUpdate(logUsual));
    }

}

